﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Kaskelot.SP.Model.Contracts;
using Kaskelot.SP.Services;
using Kaskelot.SP.Core;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using Kaskelot.SP.Model.Domain;
using System.Collections.Generic;
using System.Linq;

namespace Kaskelot.SP.UI.CONTROLTEMPLATES.Kaskelot.SP.UI.Forms
{
    public partial class FaltForm : BaseForm, IForm
    {
        public string Id { get; set; }
        public string ParentId { get; set; }
        public Falt Current { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                return;

            using (AdminService svc = new AdminService())
            {
                if (Id.IsGuid())
                    Current = svc.GetFalt(new Guid(Id));

                if (Current == null)
                {
                    Current = new Falt();
                    Current.Required = true;
                }
                else
                {
                    IList<string> res = new List<string>();
                    var v = svc.GetFaltAlternativForFalt(Current.Id);

                    foreach (FaltAlternativ f in v)
                    {
                        res.Add(f.Namn.ToString() + "\n");
                    }

                    FaltAlternativRepeater.DataSource = res;
                    FaltAlternativRepeater.DataBind();
                }
            }

            TypRepeater.DataSource = Enum.GetValues(typeof(Stockholm.SOA.Kaskelot.WCF.DataContracts.FaltTyp)).Cast<Stockholm.SOA.Kaskelot.WCF.DataContracts.FaltTyp>().Select(t => new
            {
                ID = (int)t,
                Name = t.ToString()
            });
            TypRepeater.DataBind();
        }
    }
}
